package com.sinsz.common.util;

import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

/**
 * 验证工具
 * @author chenjianbo
 */
public final class ValidateUtils {

    private static final String REG_IP = "^((25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(25[0-5]|2[0-4]\\d|(" +
            "(1\\d{2})|([1-9]?\\d)))$";

    private static final String REG_DOMAIN = "^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\" +
            ".[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";

    /**
     * IP地址正则验证
     * @param ip    ip字符串
     * @return
     */
    public static boolean isIP(String ip) {
        return StringUtils.hasText(ip) && Pattern.matches(REG_IP, ip);
    }

    /**
     * 域名地址正则验证
     * @param domain    域名地址
     * @return
     */
    public static boolean isDomain(String domain) {
        return StringUtils.hasText(domain) && Pattern.matches(REG_DOMAIN, domain);
    }

}
